/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entities.Customer;
import entities.CustomerPK;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Andrés Felipe
 */
@Stateless
@Remote(CustomerFacadeRemote.class)
public class CustomerFacade extends AbstractFacade<Customer> implements CustomerFacadeRemote {
    @PersistenceContext(unitName = "Javeriana.EJB.EstimatorPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CustomerFacade() {
        super(Customer.class);
    }    

    @Override
    public void deleteCustomer(String identification, int pymeIdentification) {
        //System.out.println("Intentaré borrar...");      
        CustomerPK key = new CustomerPK();
        key.setIdentification(identification);
        key.setPymeidentification(pymeIdentification);
        Customer customer = this.find(key);
        //System.out.println(customer.getName());
        this.remove(customer);
        
        //System.out.println("Borré!!! :D :D :D");
    }

    @Override
    public List<String> findAllClients(int pymeId) {
       List<String> result = new ArrayList<>();
        Query query = em.createNamedQuery("Customer.findByPymeidentification").setParameter("pymeidentification", pymeId);
        List<Customer> customers = query.getResultList();
        for (Customer customer : customers) {
            String temp = customer.getCustomerPK().getIdentification() + "-" + customer.getName();
            result.add(temp);
        }
        return result;
    }
}
